package com.hxzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.util.WaimaiThreadLocal;
import com.hxzy.common.vo.resp.R;
import com.hxzy.controller.admin.dicttype.req.DictTypeEditReq;
import com.hxzy.controller.admin.dicttype.req.DictTypeReq;
import com.hxzy.controller.admin.mapstruct.DictTypeConvert;
import com.hxzy.entity.SysDictData;
import com.hxzy.entity.SysDictType;
import com.hxzy.service.SysDictDataService;
import com.hxzy.service.SysDictTypeService;
import com.hxzy.mapper.SysDictTypeMapper;
import com.hxzy.util.DictCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* @author Administrator
* @description 针对表【sys_dict_type(字典类型表)】的数据库操作Service实现
* @createDate 2023-06-28 16:38:45
*/
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType>
    implements SysDictTypeService{

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    private SysDictDataService sysDictDataService;

    @Autowired
    private DictCacheUtil dictCacheUtil;

    @Override
    public PageInfo<SysDictType> search(DictTypeReq req) {
        return PageHelper.startPage(req.getPage(),req.getSize())
                .doSelectPageInfo(()-> {
                    sysDictTypeMapper.search(req);
                });
    }

    @Override
    public List<Map<String, Object>> dataAll() {
        return this.sysDictTypeMapper.dataAll();
    }

    @Override
    public boolean delete(Long[] ids) {
        SysDictType entity=new SysDictType();
        entity.setStatus("1");
        entity.setUpdateBy(WaimaiThreadLocal.getName());
        entity.setUpdateTime(new Date());

        LambdaQueryWrapper<SysDictType> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(SysDictType::getId,ids);

        boolean update = super.update(entity, queryWrapper);
        return update;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean edit(DictTypeEditReq req) {
        SysDictType db = this.getById(req.getId());
        if(Objects.isNull(db)){
            throw new ServiceException(AckCode.DATA_NOT_FOUND);
        }

        SysDictType entity= DictTypeConvert.INSTANCE.convert(req);
        boolean save = this.updateById(entity);


        //判断，如果是修改了dictType的值，就必须要更新sysDictData表的数据
        if(!req.getDictType().equals(db.getDictType())){
            this.sysDictDataService.updateDictType(db.getDictType(),req.getDictType());
            //清空以前的redis缓存，因为你的dict_type的值已经被改过了
            this.dictCacheUtil.removeByDictType(db.getDictType());
        }

        //如果是禁用状态，就要移出redis缓存中的值
        if(entity.getStatus().equals("1")){
            this.dictCacheUtil.removeByDictType(req.getDictType());
        }else{
            if(!req.getDictType().equals(db.getDictType())){
                this.dictCacheUtil.refreshCacheByDictType(req.getDictType());
            }else{
                this.dictCacheUtil.refreshCacheByDictType(db.getDictType());
            }
        }
        return save;
    }

    @Override
    public List<SysDictType> download(DictTypeReq req) {
        return this.sysDictTypeMapper.search(req);
    }
}




